package com.demo.net.ui;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private static boolean running = true;
    private static String[] stories = new String[]{
            "桃花岛山洞",
            "黄药师：老顽童，听说江湖上欧阳修得到了九阴真经上册！你不是说真经上册一直在你这么？",
            "周伯通：哦，是么？不可能吧......",
            "黄药师：你骗了我这么久！居然把真经给了别人！！！接我的碧海潮生曲！！",
            "周伯通：等等！！！我肚子痛！",
            "黄药师：·#￥！%",
            "周伯通：黄老邪，不是我怕你！真经真的还在我这！！",
            "黄药师：那拿出来让我瞧瞧！",
            "周伯通：不行，我拿出来你就抢了！",
            "黄药师：我看是你早就给人了！我们一同出去，若是让我发现真经在别人那里，哼哼~",
            "周伯通：怎么样？",
            "黄药师：我就宰了你！",
            "周伯通：......（就你那能耐，呸，打的赢我吗？）",
            "黄药师与周伯通为九阴真经出了桃花岛......",
            "三天后",
            "欧阳修被黄药师打晕了",
            "黄药师：如何，这就是从他身上搜出的真经！！！",
            "周伯通（翻了两下）：明明是无字书啊！！",
            "黄药师、欧阳修：不可能！！！",
            "欧阳修心想：我都拿了3天，看了无数遍，如何可能是无字书？？？？？？",
            "黄药师翻了两下：真的无字书！！",
            "周伯通暗自偷乐：真经原本早就被我毁了！现在的都是我默写出来的！\n",
            "我都用草汁写在纸上，一见光3天后字迹就消失！任你黄老邪奸似鬼，还不是喝我的洗脚水！",
            "黄老邪：好，只要真经不在别人那里就行，跟我回桃花岛。",
            "周伯通：你......·#￥%",
    };


    class ClientHandler extends Thread {
        Socket client;
        PrintStream ps;
        BufferedReader br;
        private int index = 0;

        ClientHandler(Socket client) {
            this.client = client;
            System.out.println("有客户端连接上来了：" + client.getRemoteSocketAddress().toString());
            try {
                ps = new PrintStream(client.getOutputStream());
                ps.println("welcome client!");
                //获取数据输入流
                br = new BufferedReader(new InputStreamReader(client.getInputStream()));
                new Thread(()->{
                    ps.println("接下来我会给你讲一个江湖传闻!");
                    while (index < stories.length){
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        ps.println(stories[index]);
                        index ++;
                    }
                    running = false;
                    try {
                        this.ps.close();
                        this.br.close();
                        this.client.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            while (running) {
                String line = null;
                try {
                    line = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (line == null) break;
                System.out.println("收到客户端内容：" + line);
                if (line.equalsIgnoreCase("exit")) {
                    ps.println("bye");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
                }
                if (line.startsWith("help")) {
                    ps.println("nihao,can i help u?");
                } else {
                    ps.println("i don't know what u want for " + line);
                }
            }
        }
    }


    void start() throws IOException {
        // 监听10086
        ServerSocket server = new ServerSocket(10086);
        System.out.println("服务开启成功，端口为：10086");
        while (running) {
            // 等待客户端连接
            Socket client = server.accept();
            new ClientHandler(client).start();
        }
    }

    public static void main(String[] args) throws IOException {
        new Server().start();
    }


}
